﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml; 

namespace System.Xml.Schema
{
    /// <summary>
    /// XML架构辅助功能
    /// </summary>
    public class XMLSchemaHelper
    {
        /// <summary>
        /// 根据XML架构验证XML文件
        /// </summary>
        /// <param name="schemaStream"></param>
        /// <param name="xml"></param>
        /// <param name="xmlValEvent"></param>
        /// <param name="schemaErrorEvent"></param>
        public static void Validate(Stream schemaStream, XmlReader xml, ValidationEventHandler xmlValEvent,ValidationEventHandler schemaErrorEvent=null)
        {
            XmlReaderSettings schemaSettings = new XmlReaderSettings();
            schemaSettings.Schemas.Add(XmlSchema.Read(schemaStream,schemaErrorEvent));
            schemaSettings.ValidationType = ValidationType.Schema;
            schemaSettings.ValidationEventHandler += xmlValEvent;
            XmlReader reader = XmlReader.Create(xml, schemaSettings);
            while (reader.Read()) { }
        }
    }
}
